package com.zl.basis.config;

import android.content.Context;
import android.util.Log;

import com.tencent.mmkv.MMKV;
import com.zl.basis.component.AppInfo;
import com.zl.basis.component.AppUtil;

public class BasisConfig {
    private static final String TAG = "Config";

    public static void initConfig() {
        initConfig(BasisConstant.CONFIG_DATA_PATH);
    }

    public static void initConfig(String configPath) {
        String dir = MMKV.initialize(configPath);
        Log.i(TAG, "initMMKV--->dir:" + dir);
    }

    public static MMKV getAppConfig() {
        return InnerAppMMKVConfig.APP_CONFIG_INFO;
    }

    public static MMKV getBasisConfig() {
        return InnerBasisMMKVConfig.BASIS_CONFIG_INFO;
    }

    public static MMKV getApiConfig() {
        return InnerApiMMKVConfig.API_CONFIG_INFO;
    }

    public static class InnerAppMMKVConfig {
        private static MMKV APP_CONFIG_INFO = MMKV.mmkvWithID("AppConfigInfo", MMKV.MULTI_PROCESS_MODE);
    }

    public static class InnerBasisMMKVConfig {
        private static MMKV BASIS_CONFIG_INFO = MMKV.mmkvWithID("BasisConfigInfo", MMKV.MULTI_PROCESS_MODE);
    }

    public static class InnerApiMMKVConfig {
        private static MMKV API_CONFIG_INFO = MMKV.mmkvWithID("ApiConfigInfo", MMKV.MULTI_PROCESS_MODE);
    }

    public static int getAppVersionCode() {
        return getBasisConfig().decodeInt("basis_app_version_code");
    }

    public static void setAppVersionCode(int appVersionCode) {
        getBasisConfig().encode("basis_app_version_code", appVersionCode);
    }

    public static String getAppVersionName() {
        return getBasisConfig().decodeString("basis_app_version_name");
    }

    public static void setAppVersionName(String appVersionName) {
        getBasisConfig().encode("basis_app_version_name", appVersionName);
    }


    public static String getAppPackageName() {
        return getBasisConfig().decodeString("basis_app_package_name");
    }

    public static void setAppPackageName(String packageName) {
        getBasisConfig().encode("basis_app_package_name", packageName);
    }

    public static AppInfo getAppInfo() {
        AppInfo appInfo = getBasisConfig().decodeParcelable("basis_app_info", AppInfo.class);
        return appInfo;
    }

    public static AppInfo getAppInfo(Context mContext) {
        AppInfo appInfo = getBasisConfig().decodeParcelable("basis_app_info", AppInfo.class);
        if (null == appInfo) {
            appInfo = AppUtil.getAppInfo(mContext);
            setAppInfo(appInfo);
        }
        return appInfo;
    }


    public static void setAppInfo(AppInfo appInfo) {
        getBasisConfig().encode("basis_app_info", appInfo);
        setAppVersionName(appInfo.getVersionName());
        setAppVersionCode(appInfo.getVersionCode());
        setAppPackageName(appInfo.getPackageName());
    }

    public static boolean isNetWorkState() {
        return getBasisConfig().decodeBool("basis_net_work_state", false);
    }

    public static void setNetWorkState(boolean netWorkState) {
        getBasisConfig().encode("basis_net_work_state", netWorkState);
    }
}
